﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// 使用表达式树创建对像
/// http://msdn.microsoft.com/zh-cn/library/bb397951(v=vs.110).aspx
/// </summary>
public partial class ExpressionTree表达式树_ExpressionTree创建对像aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //http://msdn.microsoft.com/zh-cn/library/bb397951(v=vs.110).aspx
        Func<object> funcT = CreateInstance(this.GetType());
        //创建一个表达式对像
        var ddd = funcT();
        Response.Write(string.Format("用表达式树创建了一个类型:{0}", ddd));
        Response.Write("<br/>");


        //分析表达式
        System.Linq.Expressions.Expression express = System.Linq.Expressions.Expression.New(this.GetType());
        System.Linq.Expressions.Expression<Func<object>> exprTree = System.Linq.Expressions.Expression.Lambda<Func<object>>(express);
        System.Linq.Expressions.Expression<Func<object>> exp2 = () => new ExpressionTree表达式树_ExpressionTree创建对像aspx();



        Response.Write(string.Format("exprTree:{0}", exprTree));
        Response.Write("<br/>");
        Response.Write(string.Format("exprTree:{0}", exp2));
        Response.Write("<br/>");
        Response.Write(string.Format("表达式分析:{0}", 分析表达式()));
    }

    /// <summary>
    ///创建一个指定的类型
    /// </summary>
    /// <param name="type">类型</param>
    /// <returns>返回创建类型的委托</returns>
    public Func<object> CreateInstance(Type type)
    {
        System.Linq.Expressions.Expression express = System.Linq.Expressions.Expression.New(type);
        System.Linq.Expressions.Expression<Func<object>> result = System.Linq.Expressions.Expression.Lambda<Func<object>>(express);

        Func<object> func = result.Compile();
        return func;
    }


    public string 分析表达式()
    {

        //ParameterExpression numParam = Expression.Parameter(typeof(int), "num");
        //ConstantExpression five = Expression.Constant(5, typeof(int));
        //BinaryExpression numLessThanFive = Expression.LessThan(numParam, five);
        //Expression<Func<int, bool>> lambda1 =
        //    Expression.Lambda<Func<int, bool>>(
        //        numLessThanFive,
        //        new ParameterExpression[] { numParam });


        Expression<Func<int, bool>> exprTree = num => num < 5;

        // Decompose the expression tree.
        ParameterExpression param = (ParameterExpression)exprTree.Parameters[0];
        BinaryExpression operation = (BinaryExpression)exprTree.Body;
        ParameterExpression left = (ParameterExpression)operation.Left;
        ConstantExpression right = (ConstantExpression)operation.Right;
        string output = string.Format("Decomposed expression: {0} => {1} {2} {3}",
        param.Name, left.Name, operation.NodeType, right.Value);
        Console.WriteLine(output);
        return output;
    }


}